package com.css.dzj.dzjportal.modules.updown.controller;




import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.dzj.dzjportal.common.util.R;
import com.css.dzj.dzjportal.common.util.ServiceUtil;
import com.css.dzj.dzjportal.common.util.http.HttpClientUtil;
import com.css.dzj.dzjportal.modules.updown.entity.CommAttachmentEntity;
import com.css.dzj.dzjportal.modules.updown.service.AttachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ud")
@PropertySource(value = "classpath:httpurl.properties")
public class UpdAndDownController {
    @Autowired
    private AttachService attachService;
    //安评系统url
    @Value("${AP_URL}")
    private  String apRestUrl;
    //外国人来华系统url
    @Value("${WGR_URL}")
    private  String wgrRestUrl;
    //项目模块下所有附件目录
    @RequestMapping("/selectId")
    public R selectAllId(String id) {
        List<CommAttachmentEntity> attachmentEntities = attachService.queryAll(id);
        if (attachmentEntities != null && attachmentEntities.size() > 0) {
            return R.ok().put("data", attachmentEntities);
        }
        return R.error("附件未上传");
    }

    @RequestMapping("/save")
    public R save(@RequestParam(name = "file") MultipartFile file, @RequestParam(name = "id") String id) {
        try {
            String s = attachService.save(file, id);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("上传错误");
        }
        return R.ok().put("data", "ok");
    }

    @RequestMapping("/download")
    public String download(String id) {
        try {
            attachService.download(id);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("下载出现错误").toString();
        }
        return R.ok().put("data", "ok").toString();
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public R del(String id) {
        String resultInfo = attachService.del(id);
        return R.ok().put("data", resultInfo);
    }
    //公示公告模块下所有系统附件目录
    @RequestMapping("/selectOneId")
    public R selectOneId(String id,String appId) {
        List<CommAttachmentEntity> attachmentEntities = new ArrayList<CommAttachmentEntity>();
        JSONArray jsonObject = new JSONArray();
        if("".equals(appId)){
            String url = apRestUrl+"/ud/selectOneId?id="+id;
            String result = HttpClientUtil.doPost(url);
            if(result!=null&&result!="") {
                jsonObject = JSON.parseArray(result);
            }
        }
        if("1".equals(appId)){
            String url = wgrRestUrl+"/ud/selectOneId?id="+id;
            String result = HttpClientUtil.doPost(url);
            if(result!=null&&result!="") {
                jsonObject = JSON.parseArray(result);
            }
        }
        if (jsonObject != null) {
            for (int i=0;i<jsonObject.size();i++){
                CommAttachmentEntity commAttachmentEntity = new CommAttachmentEntity();
                commAttachmentEntity=(CommAttachmentEntity)JSONObject.parseObject(jsonObject.get(i).toString(), CommAttachmentEntity.class);
                attachmentEntities.add(commAttachmentEntity);
            }
            return R.ok().put("data", attachmentEntities);
        }
        return R.error("附件未上传");
    }

    /**
     * 下载非本系统附件
     * @param Filename
     * @param Fileurl
     * @param TableName
     * @return
     */
    @RequestMapping("/downloadPublic")
    public String downloadPublic(String Filename,String Fileurl,String TableName) {
        try {
            CommAttachmentEntity commAttachmentEntity = new CommAttachmentEntity();
            commAttachmentEntity.setFilename(Filename);
            commAttachmentEntity.setFileurl(Fileurl);
            commAttachmentEntity.setTableName(TableName);
            attachService.downloadPublic(commAttachmentEntity);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("下载出现错误").toString();
        }
        return R.ok().put("data", "ok").toString();
    }

    /**
     * 上传投诉举报附件
     * @param file
     * @param id
     * @return
     */
    @PostMapping("/saveTsjbfj")
    public R saveTsjbfj(@RequestParam(name = "file") MultipartFile file, @RequestParam(name = "id") String id) {
        try {
            String s = attachService.saveTsjbfj(file, id,wgrRestUrl);
            if("200".equals(s)){
                return R.ok();
            }else {
                return R.error("上传失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("上传错误");
        }
    }

    /**
     * 查询投诉举报附件
     * @param id
     * @return
     */
    @GetMapping("/selecttsjbId")
    public R selectPage(String id) {
        String url=wgrRestUrl+"/ud/selectId";
        HashMap<String,String> map=new HashMap<>();
        map.put("id",id);
        String msg=HttpClientUtil.doPost(url,map);
        JSONObject jsonObject=(JSONObject)JSON.parse(msg);
        System.out.println(jsonObject.get("code"));
        if(jsonObject.get("code").equals(200)){
            return R.ok().put("data",jsonObject.get("data"));
        }else {
            return R.error("查询失败");
        }
    }

    /**
     * 删除投诉举报附件
     * @param id
     * @return
     */
    @RequestMapping(value = "/deltsjbfj", method = RequestMethod.POST)
    public R deltsjbfj(String id) {
        String url=wgrRestUrl+"/ud/del";
        HashMap<String,String> map=new HashMap<>();
        map.put("id",id);
        String msg=HttpClientUtil.doPost(url,map);
        JSONObject jsonObject=(JSONObject)JSON.parse(msg);
        if(jsonObject.get("code").equals(200)){
            return R.ok();
        }else {
            return R.error("删除失败");
        }
    }
}
